package cn.circle.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.circle.system.dao.ICommodityCategoryDao;
import cn.circle.system.model.CommodityCategory;
import cn.circle.system.service.ICommodityCategoryService;
import cn.circle.system.model.SysCustomer;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.util.HqlHelper;

/**
 * 实现CommodityCategory接口方法 
 */
@Service
public class CommodityCategoryServiceImpl implements ICommodityCategoryService {
	
	@Autowired
	private ICommodityCategoryDao commodityCategoryDao;

	@Override
	public boolean save(CommodityCategory commodityCategory) {
		commodityCategoryDao.save(commodityCategory);
		return false;
	}

	@Override
	public List<CommodityCategory> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteAll(String ids) {
		commodityCategoryDao.deleteAll(ids);
		return false;
	}

	@Override
	public boolean removeAll(String ids) {
		commodityCategoryDao.removeAll(ids);
		return false;
	}

	@Override
	public List<CommodityCategory> getOne(String id) {
		String hql = "from CommodityCategory s where s.categoryId="+id;
		List<CommodityCategory> c = commodityCategoryDao.find(hql);
		return c;
	}

	@Override
	public boolean update(CommodityCategory commodityCategory) {
		commodityCategoryDao.updateAll(commodityCategory);
		return false;
	}

	@Override
	public DataGrid datagrid(PageHelper ph, CommodityCategory c) {
		DataGrid dg = new DataGrid();
		List<CommodityCategory> cml = new ArrayList<CommodityCategory>();
		//用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		
		//注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer(
				"from CommodityCategory t where 1=1 ");
		
		//以下为查询条件
		if(c.getName() != null && !"".equals(c.getName())){
			String loginName = null;
			try {
				loginName = URLDecoder.decode(c.getName(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("name", "%" + loginName + "%");
			hql.append(" and t.name like :name ");
		}
		
		//hql = HqlHelper.orderHql(hql, ph); // 排序
		List<CommodityCategory> l = commodityCategoryDao.find(hql.toString(), params, ph.getPage(),
				ph.getRows());
		dg.setRows(l);
		dg.setTotal(commodityCategoryDao.count("select count(*) " + hql, params));
		return dg;
		
	}

	@Override
	public List<CommodityCategory> getCategory() {
		String hql = " from CommodityCategory c where c.status = 9";
		return commodityCategoryDao.find(hql);
	}
	
	
	
}
